//
//  SgyCollectionViewCell.m
//  PAChat
//
//  Created by ShiMac on 15/6/3.
//  Copyright (c) 2015年 FreeDo. All rights reserved.
//

#import "SgyCollectionViewCell.h"

@interface SgyCollectionViewCell ()

@end

@implementation SgyCollectionViewCell

-(id)initWithFrame:(CGRect)frame
{
    if (self=[super initWithFrame:frame]) {
        
        self.imageView=[[UIImageView alloc]initWithFrame:CGRectZero];
        self.imageView.userInteractionEnabled=YES;
        self.imageView.clipsToBounds=YES;
        self.imageView.contentMode=UIViewContentModeScaleAspectFill;
        [self addSubview:self.imageView];
        
        self.selectBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [self.selectBtn setImage:[UIImage imageNamed:@"selected"] forState:UIControlStateSelected];
        [self.selectBtn setImage:[UIImage imageNamed:@"unselect"] forState:UIControlStateNormal];
        
        [self.selectBtn addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.imageView addSubview:self.selectBtn];
        
        
        self.videoIcon=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"GMVideoIcon"]];
        [self.imageView addSubview:self.videoIcon];
        self.timeLabel=[[UILabel alloc] initWithFrame:CGRectZero];
        self.timeLabel.textColor=[UIColor whiteColor];
        self.timeLabel.font=[UIFont systemFontOfSize:12];
        self.timeLabel.textAlignment=NSTextAlignmentRight;
        [self.imageView addSubview:self.timeLabel];
        
        self.timeLabel.hidden=YES;
        self.videoIcon.hidden=YES;
        
        
    }
    return self;
}

- (void)setThumbnailImage:(UIImage *)thumbnailImage {

    self.imageView.image = thumbnailImage;
}

-(void)layoutSubviews{
    
    [super layoutSubviews];
    self.imageView.frame=CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
    self.selectBtn.frame=CGRectMake(self.imageView.frame.size.width-26-5, self.imageView.frame.size.height-26-5, 30, 30);
    self.videoIcon.frame=CGRectMake(2, self.imageView.frame.size.height-10, 15, 8);
    self.timeLabel.frame=CGRectMake(CGRectGetMaxX(self.videoIcon.frame)+3,self.frame.size.height-12, self.frame.size.width-22, 10);
}

-(void)selectAction:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(didChangeValueForKey:)]) {
        
       BOOL result= [self.delegate didSelectCell:sender];
        if (result) {
            
            if (!sender.selected) {
                
                CAKeyframeAnimation *keyAnimation=(CAKeyframeAnimation *)[sender.layer animationForKey:@"ddd"];
                if (!keyAnimation) {
                    keyAnimation= [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
                    keyAnimation.values=@[[NSNumber numberWithFloat:1.2],
                                          [NSNumber numberWithFloat:0.8],
                                          [NSNumber numberWithFloat:1]];
                    
                    keyAnimation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
                    keyAnimation.duration=0.5;
                }
                
                [sender.layer addAnimation:keyAnimation forKey:@"ddd"];
                
                
            }
            
            
            sender.selected=!sender.selected;
            
            
            
        }else{
            
            
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed Selected" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            
            [alert show];
        }
        
    
    
    }
    
 

    
}

-(NSString*)getDurationWithFormat:(NSTimeInterval)duration
{
    NSInteger ti = (NSInteger)duration;
    NSInteger seconds = ti % 60;
    NSInteger minutes = (ti / 60) % 60;
    //NSInteger hours = (ti / 3600);
    return [NSString stringWithFormat:@"%02ld:%02ld", (long)minutes, (long)seconds];
}

@end